package com.chainwintel.prjmgmt.model;

import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Transient;

@Entity
public class MenuItem {
	
	private long id;
	
//	private String strId;
	
	private String name;
	
	private String url;
	
	private MenuItem parentMenu;
	
	private Set<MenuItem> childMenus;
	
	private NumberFormat nf = NumberFormat.getIntegerInstance();
	
//	private Long version;
	
//	private MenuCategory category;

	
	
	public MenuItem() {
	}

	public MenuItem(String text, String url, MenuItem parentItem) {
		// TODO Auto-generated constructor stub
		this.name = text;
		this.url = url;
		this.parentMenu = parentItem;
	}

	@Id @GeneratedValue//(strategy = GenerationType.TABLE, generator = "hilo")
	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	@ManyToOne
	public MenuItem getParentMenu() {
		return parentMenu;
	}

	public void setParentMenu(MenuItem parentMenu) {
		this.parentMenu = parentMenu;
	}

	@OneToMany(mappedBy = "parentMenu", cascade = CascadeType.ALL)
	public Set<MenuItem> getChildMenus() {
		return childMenus;
	}

	public void setChildMenus(Set<MenuItem> childMenus) {
		this.childMenus = childMenus;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	/**
	 * DOJO tree 节点ID在render时会将数字类型值格式化，形成一个带千位分隔符的string，
	 * 需要重新转换成long类型。
	 * @param strId the strId to set
	 */
//	public void setStrId(String strId) {
//		try {
//			this.strId = strId;
//			if (strId != null && !strId.isEmpty()) {
//				setId((Long) nf.parse(strId));
//			}
//		} catch (ParseException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}	
//	}

//	/**
//	 * @return the strId
//	 */
//	@Transient
//	public String getStrId() {
//		return strId;
//	}

	/**
	 * @param category the category to set
	 */
/*	public void setCategory(MenuCategory category) {
		this.category = category;
	}*/

	/**
	 * @return the category
	 */
/*	@ManyToOne
	public MenuCategory getCategory() {
		return category;
	}*/

/*	protected void setVersion(Long version) {
		this.version = version;
	}*/

/*	@Version
	protected Long getVersion() {
		return version;
	}*/

}
